$(function () {
    //获取所有的增加按钮
    var increment = document.getElementsByClassName("increase");
    //给这些按钮加index属性,以及增加功能
    for(var i = 0 ; i < increment.length ; i++){
        //添加index属性
        increment[i].index = i;
        //添加功能函数
        increment[i].onclick = function () {
            console.log("I'm trying to add increase function")
            //获取当前索引
            var currentIndex = this.index;
            //获取对应的数量框
            var currentQuantity = document.getElementsByClassName("quantity")[currentIndex];
            //改变值
            /**可考虑添加与数据库的库存进行比较?**/
            var newNumber = parseInt(currentQuantity.value) + 1;
            currentQuantity.setAttribute('value',newNumber);
            console.log(newNumber);
            //更新单品总价
            caculateSingleItemTotalPrice(currentIndex,newNumber);
        }
    }

    //获取所有的减少按钮
    var decrement = document.getElementsByClassName("decrease");
    //给这些按钮加index属性,以及减少功能
    for(var i = 0 ; i < decrement.length ; i++){
        //添加index属性
        decrement[i].index = i;
        //添加功能函数
        decrement[i].onclick = function () {
            console.log("I'm trying to add decrease function")
            //获取当前索引
            var currentIndex = this.index;
            //获取对应的数量框
            var currentQuantity = document.getElementsByClassName("quantity")[currentIndex];
            //改变值
            var newNumber = parseInt(currentQuantity.value) - 1;
            if(newNumber >= 0){
                currentQuantity.setAttribute('value',newNumber);
            }else{
                newNumber=0
                alert("Can't reduce more");
            }
            console.log(newNumber);
            //更新单品总价
            caculateSingleItemTotalPrice(currentIndex,newNumber);
        }
    }

    //获取所有的商品价格输入框
    var quantityInput = document.getElementsByClassName("quantity");
    //给这些输入框加index属性,以及计算总价功能
    for(var i = 0 ; i < quantityInput.length ; i++){
        //添加index属性
        quantityInput[i].index = i;
        //添加功能函数
        quantityInput[i].oninput = function () {
            console.log("I'm trying to add changePrice function")
            //获得购买数量
            var newNumber = parseInt(this.value);
            console.log(newNumber)
            //获取当前索引
            var currentIndex = this.index;
            caculateSingleItemTotalPrice(currentIndex,newNumber)
        }
    }

    function caculateSingleItemTotalPrice(currentItemIndex,itemNumber) {
        //获取对应的单价文本框
        var currentListPriceInput = document.getElementsByClassName("listPrice")[currentItemIndex];
        //获得对应产品的单价
        var currentListPrice = currentListPriceInput.innerHTML;
        currentListPrice = currentListPrice.trim();
        currentListPrice = currentListPrice.substring(1);
        console.log(currentListPrice)
        //计算总价
        var totalPrice = currentListPrice * itemNumber;
        console.log(totalPrice)
        //更新单品总价
        var totalPriceInput = document.getElementsByClassName("totalPrice")[currentItemIndex];
        totalPriceInput.innerHTML = "$"+ totalPrice.toFixed(2);
        //更新总价
        caculateSubTotal();
    }

    function caculateSubTotal() {
        //获得所有的总价
        var allTotalPrice = document.getElementsByClassName("totalPrice");
        var totalPrice= 0;
        for(var i = 0 ; i < allTotalPrice.length ; i++){
            var innerPrice = allTotalPrice[i].innerHTML;
            console.log("total Price" + i + "="+ totalPrice)
            innerPrice = innerPrice.trim();
            innerPrice = innerPrice.substring(1);
            totalPrice = parseFloat(innerPrice) + parseFloat(totalPrice);
        }
        console.log("total Price" + totalPrice)
        var subTotal = document.getElementById("subTotal");
        subTotal.innerHTML = "Sub Total:$" + totalPrice.toFixed(2);

    }

});